package cn.stylefeng.guns.modular.demos.controller;

import cn.stylefeng.guns.modular.demos.entity.EduAttendanceInfo;
import cn.stylefeng.guns.modular.demos.entity.EduCourseSelectionInfo;
import cn.stylefeng.guns.modular.demos.model.params.EduCourseSelectionInfoParam;
import cn.stylefeng.guns.modular.demos.service.EduAttendanceInfoService;
import cn.stylefeng.guns.modular.demos.service.EduCourseSelectionInfoService;
import cn.stylefeng.guns.modular.demos.service.EduStudentInfoService;
import cn.stylefeng.guns.modular.wechat.WechatCorpMgr;
import cn.stylefeng.roses.core.base.controller.BaseController;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Controller
public class AttendanceController extends BaseController {

	private final static Logger logger = LoggerFactory.getLogger(AttendanceController.class);

    @Autowired
    private EduStudentInfoService eduStudentInfoService;

    @Autowired
	private EduCourseSelectionInfoService eduCourseSelectionInfoService;

    @Autowired
    private EduAttendanceInfoService eduAttendanceInfoService;

	private int corpAgentId = 1000006;// A组
	private static String domain = "continuefly.top:8080";

	private static final String CORP_ID = WechatCorpMgr.CORP_ID;

	@RequestMapping(value = "/code", method = RequestMethod.GET)
	public String code(Model model, HttpServletRequest request) {
		String sState = request.getParameter("state");
		logger.info("state=" + sState);
		model.addAttribute("state", sState);
		return "/code.html";
	}

	@RequestMapping(value = "/qrcode", method = RequestMethod.GET)
	public String qrcode(Model model, HttpServletRequest request) {

		String sState = request.getParameter("state");
		logger.info("state=" + sState);

		model.addAttribute("appid", CORP_ID);
		model.addAttribute("agentid", corpAgentId);
		model.addAttribute("redirect_uri", "http%3A%2F%2F" + domain + "/attendance");
		model.addAttribute("state", sState);
		model.addAttribute("refresh", (60 + Math.random() * 60));
		model.addAttribute("tagId", "");
		model.addAttribute("sucess", "");

		model.addAttribute("href",
				"data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDMwMHB4O2JvcmRlci1zdHlsZTogc29saWQ7Ym9yZGVyLXdpZHRoOiA1cHg7Ym9yZGVyLWNvbG9yOiAjZjUwOTA5O30KICAgICAuaW1wb3dlckJveCAudGl0bGUge2Rpc3BsYXk6IG5vbmU7fQogICAgIC5pbXBvd2VyQm94IC5pbmZvIHt3aWR0aDogMzAwcHg7fQogICAgIC5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZSAgIWltcG9ydGFudH0KICAgICAuaW1wb3dlckJveCAuc3RhdHVzIHt0ZXh0LWFsaWduOiBjZW50ZXI7fQog");
		return "/qrcode.html";

	}

	@RequestMapping(value = "/attendance", method = RequestMethod.GET)
	public String attendance(Model model, HttpServletRequest request) throws JSONException {

		String code = super.getPara("code").trim();
		String state = super.getPara("state");// 可能是教师名工号，也可能是教室名字，或者是选课编号; curriculum:10285-scst-2021-2022-1-SOEN7858

		logger.info("##code=" + code + " state=" + state);

		String userAccount = WechatCorpMgr.getInstance().getUserIdByCode(code, corpAgentId);
		logger.info("##userAccount=" + userAccount);
		JSONObject jsonObject = WechatCorpMgr.getInstance().getUserdByUserId(userAccount, corpAgentId);

		String mobile = jsonObject.getString("mobile");
		String name = jsonObject.getString("name");
		String avatar = jsonObject.getString("avatar");
		//更新签到状态
		EduCourseSelectionInfoParam query = new EduCourseSelectionInfoParam();
		query.setMobile(mobile);
		query.setCurriculumId(state);
		EduCourseSelectionInfo eduCourseSelectionInfoResult = eduCourseSelectionInfoService.getById(state);
		EduCourseSelectionInfoParam eduCourseSelectionInfoParam = new EduCourseSelectionInfoParam();
		eduCourseSelectionInfoParam.setUniqueKey(eduCourseSelectionInfoResult.getUniqueKey());
		eduCourseSelectionInfoParam.setStatus(EduCourseSelectionInfo.Status.SIGN.getValue());
		eduCourseSelectionInfoService.update(eduCourseSelectionInfoParam);

		//生成签到记录
		EduAttendanceInfo eduAttendanceInfo = new EduAttendanceInfo();
		BeanUtils.copyProperties(eduCourseSelectionInfoResult,eduAttendanceInfo);
		eduAttendanceInfo.setRefKey(eduCourseSelectionInfoResult.getUniqueKey());
		eduAttendanceInfo.setUniqueKey("");
		eduAttendanceInfo.setStatus(EduCourseSelectionInfo.Status.SIGN.getValue());
		eduAttendanceInfo.setSignTime(LocalDateTime.now());
		eduAttendanceInfoService.save(eduAttendanceInfo);

		//TODO, get User Name & avatar from local DB
		
		// TODO
		model.addAttribute("sucess", "签到成功");
		model.addAttribute("attendance", "/qrcode");
		model.addAttribute("name",name);
		model.addAttribute("tagId",state);
		model.addAttribute("state", state);
		model.addAttribute("avatar", avatar);
		model.addAttribute("appid", CORP_ID);
		model.addAttribute("agentid", corpAgentId);
		model.addAttribute("redirect_uri", "http%3A%2F%2F" + domain + "/attendance");
		model.addAttribute("refresh", (60 + Math.random() * 60));
		model.addAttribute("href",
				"data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDMwMHB4O2JvcmRlci1zdHlsZTogc29saWQ7Ym9yZGVyLXdpZHRoOiA1cHg7Ym9yZGVyLWNvbG9yOiAjZjUwOTA5O30KICAgICAuaW1wb3dlckJveCAudGl0bGUge2Rpc3BsYXk6IG5vbmU7fQogICAgIC5pbXBvd2VyQm94IC5pbmZvIHt3aWR0aDogMzAwcHg7fQogICAgIC5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZSAgIWltcG9ydGFudH0KICAgICAuaW1wb3dlckJveCAuc3RhdHVzIHt0ZXh0LWFsaWduOiBjZW50ZXI7fQog");

		return "/attendance.html";
	}



	@RequestMapping(value = "/attendance1", method = RequestMethod.GET)
	public String attendance1(Model model, HttpServletRequest request) throws JSONException {


		model.addAttribute("sucess", "签到成功");
		model.addAttribute("attendance", "/qrcode");
		model.addAttribute("name","");
		model.addAttribute("tagId","");
		model.addAttribute("state", "");
		model.addAttribute("avatar", "");
		model.addAttribute("appid", CORP_ID);
		model.addAttribute("agentid", corpAgentId);
		model.addAttribute("redirect_uri", "http%3A%2F%2F" + domain + "/attendance");
		model.addAttribute("refresh", (60 + Math.random() * 60));
		model.addAttribute("href",
				"data:text/css;base64,LmltcG93ZXJCb3ggLnFyY29kZSB7d2lkdGg6IDMwMHB4O2JvcmRlci1zdHlsZTogc29saWQ7Ym9yZGVyLXdpZHRoOiA1cHg7Ym9yZGVyLWNvbG9yOiAjZjUwOTA5O30KICAgICAuaW1wb3dlckJveCAudGl0bGUge2Rpc3BsYXk6IG5vbmU7fQogICAgIC5pbXBvd2VyQm94IC5pbmZvIHt3aWR0aDogMzAwcHg7fQogICAgIC5zdGF0dXNfaWNvbiB7ZGlzcGxheTogbm9uZSAgIWltcG9ydGFudH0KICAgICAuaW1wb3dlckJveCAuc3RhdHVzIHt0ZXh0LWFsaWduOiBjZW50ZXI7fQog");

		return "/attendance.html";
	}

}
